package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.Order;
import com.itheima.reggie.domain.OrderDetail;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.AddressService;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private AddressService addressService;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CartMapper cartMapper;
    //查询订单明细
    @Override
    public void submit(Order orderParam) {

//1. 准备数据备用
        //1-1 根据地址id查询地址信息
        Address address = addressService.findById(orderParam.getAddressId());
        //1-2 查询当前用户的购物车信息
        List<Cart> cartList = cartService.findList();
//2. 收到生成一个订单id
        Long orderId = IdWorker.getId();
        BigDecimal sum = new BigDecimal(0);
        for (Cart cart : cartList) {
            //3. 生成订单详情
            //每一个cart对象就应该生成一个orderDetail
            OrderDetail orderDetail = new OrderDetail();
//拷贝cart中大部分属性到orderDetail
BeanUtils.copyProperties(cart,orderDetail);
            //单独设置id和orderid
            orderDetail.setOrderId(orderId);
            orderDetail.setId(IdWorker.getId());
            sum = sum.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
//将订单详情保存数据库
            orderDetailMapper.insert(orderDetail);
        }
        //设置订单
        //4. 生成订单
        orderParam.setId(orderId);
        orderParam.setNumber(orderId+"");
        orderParam.setStatus(1);
        orderParam.setUserId(UserHolder.get().getId());
        orderParam.setOrderTime(new Date());
        orderParam.setCheckoutTime(new Date());
        orderParam.setPayMethod(1);
        orderParam.setAmount(sum);

        orderParam.setPhone(address.getPhone());
        orderParam.setAddress(address.getDetail());
        orderParam.setUserName(UserHolder.get().getName());
        orderParam.setConsignee(address.getConsignee());
        orderMapper.insert(orderParam);
        cartService.remove();
    }
//查询历史订单
    @Override
    public Page<Order> findByPage(Integer pageNum, Integer pageSize) {
        //设置分页条件
        Page<Order> page = new Page<>(pageNum,pageSize);
        //根据时间排序
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getUserId, UserHolder.get().getId());
        orderMapper.selectPage(page, queryWrapper);
        List<Order> orderList = page.getRecords();
        //查询菜品
        if (CollectionUtil.isNotEmpty(orderList)) {
            for (Order order : orderList) {
                LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(OrderDetail::getOrderId, order.getId());
                List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapper);
                order.setOrderDetails(orderDetails);
            }
        }

        return page;
    }
//订单明细
    @Override
    public Page<Order> findByPageFroManage(Integer pageNum, Integer pageSize, String number, Date beginTime, Date endTime) {
//设置分页条件 \
        Page<Order> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(number),Order::getNumber, number)
                .between(beginTime != null, Order::getOrderTime, beginTime, endTime);
        return orderMapper.selectPage(page, queryWrapper);
    }
}
